import * as VueRouter from 'vue-router'
import { routes } from './routes'
import testTools from '@test/tools'

const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes: [
        {
            path: '/signin',
            name: 'signin',
            component: () => import('@/views/SignIn.vue')
        },
        {
            path: '/',
            name: 'index',
            component: () => import('@/components/Index.vue'),
            children: [{
                path: 'home',
                name: 'home',
                component: () => import('@/views/Home.vue')
            },
            ...routes
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    const userInfo = testTools.local.get('userInfo')
    if (to.fullPath === '/signin') {
        if (userInfo && userInfo.token) {
            return next('/home')
        }
    } else {
        if (!userInfo || !userInfo.token) {
            return next('/signin')
        }
    }
    next()
})

export default router